
// This class is the main class for our chatbox.
// From here, we can get instances of classes, and the dialogs 'main' and 'contacts'.
// It also controls the login and logout procedures.

var ChatboxMain = new Class({
	
	// Get and set the dialogs main and contacts.
	getDlgMain: function()  { return this.dlgMain; }, // end function getDlgMain
	getDlgContacts: function()  { return this.dlgContacts; }, // end function getDlgContacts
	setDlgMain: function(c) { this.dlgMain = c; }, // end function setDlgMain
	setDlgContacts: function(c) { this.dlgContacts = c; }, // end function setDlgContacts
	
	// Functions for retrieving some instances from the outside.
	getClsContacts: function() 		{ return this.contacts; 	}, // end function getClsContacts
	getClsChatGroup: function() 	{ return this.chatgroup; 	}, // end function getClsChatGroup
	getClsChatPerson: function() 	{ return this.chatperson; 	}, // end function getClsChatPerson
	getClsChat: function() 			{ return this.chat; 		}, // end function getClsChat
	getClsLoop: function() 			{ return this.loop; 		}, // end function getClsLoop
	getClsNickname: function() 		{ return this.nickname; 	}, // end function getClsNickname
	
	// Start loading the chatbox modus.
	initialize: function(m)
	{
		// Are we logged in? 
		if (m.getClsSession().getStatus() == -1)
			
			// No, you can't start a chatbox modus without a session.
			return;
		
		// Save the main clases (deprecated)
		// TODO: Remove this.
		this.main = m;
		
		// Start chatbox loop
		this.contacts   = new ChatboxContacts();
		this.chatgroup  = new ChatboxGroup();
		this.chatperson = new ChatboxPerson();
		this.chat       = new ChatboxChat();
		this.loop       = new ChatboxLoop();
		this.nickname   = new ChatboxNickname(null);
		
		// Show main window
		cimport('panels.Chatbox.Main', function(atr)
		{
			// Create a new instance of the dialog main.
			var dlgMain = new DlgChatboxMain(atr);
			
			// Create and show the dialog
			atr.getClsDialog().create(dlgMain);
			
			// Save the instance 
			atr.getChat().setDlgMain(dlgMain);
			
			// Start the synchronizing loop.
			atr.getChat().getClsLoop().start();
			
			// Set the nickname right
			atr.getChat().getClsNickname().setNickname(chat.getClsSession().getNickname());
			
			// Load/update the contacts list.
			atr.getChat().getClsContacts().update();
			
			// Show contacts after  main panel is loaded
			cimport('panels.Chatbox.Contacts', function(atr)
			{
				// Create a new instance of the dialog contacts.
				var dlgContacts = new DlgChatboxContacts(atr);
				
				// Create and show the dialog
				atr.dialogs.create(dlgContacts);
				
				// Save the instance
				atr.getChat().setDlgContacts(dlgContacts);
				
			}, atr);
		
		}, m);
	},
	
	// Make the chatbox modus active again
	// This function is triggered when there was an logout procedure,
	// and someone (else) logs in.
	makeActive: function()
	{
		// Update the dialogs
		this.dlgMain.update();
		this.dlgContacts.update();
		
		// Show the dialogs again
		chat.getClsDialog().show(this.dlgMain.getID());
		chat.getClsDialog().show(this.dlgContacts.getID());
		
		// Update the nickname class
		this.nickname.setNickname(chat.getClsSession().getNickname());
		
		// Update the contacts list
		this.contacts.update();
		
		// Start the synchronizing loop again.
		this.loop.start();
		
	},
	
	// Logout function, it kicks all the dialogs and resets the classes.
	exit: function()
	{
		// Close the dialogs contacts and main
		chat.getClsDialog().close(this.dlgMain.getID());
		chat.getClsDialog().close(this.dlgContacts.getID());
		
		// Reset the nickname info
		this.getClsNickname().reset();
		
		// Reset the contacts list (cache)
		this.getClsContacts().reset();
		
		// Close all the chat windows and end conversations.
		this.getClsChat().reset();
	},
});

// Load sequence functions.
chatbox_load_1 		= function() {  	cimport('chatbox.Contacts', 		chatbox_load_2, null); 		}
chatbox_load_2 		= function() {  	cimport('chatbox.ChatGroup', 		chatbox_load_3, null); 		}
chatbox_load_3 		= function() {  	cimport('chatbox.ChatPerson', 		chatbox_load_4, null); 		}
chatbox_load_4 		= function() {  	cimport('chatbox.Chat', 			chatbox_load_5, null); 		}
chatbox_load_5 		= function() {  	cimport('chatbox.Loop', 			chatbox_load_6, null); 		}
chatbox_load_6 		= function() {  	cimport('chatbox.Nickname', 		chatbox_load_done, null); 		}
//chatbox_load_7 		= function() {  	cimport('panels.Chatbox.Main', 		chatbox_load_8, null); 		}
//chatbox_load_8 		= function() {  	cimport('panels.Chatbox.Contacts', 	chatbox_load_done, null); 	}
chatbox_load_done 	= function() { 	cimport_loaded('chatbox.Main'); 								}
chatbox_load_1();